package com.dudu.utils;

import java.util.Random;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.dudu.access.RedisUtil;

/**
 * 工具类
 * 
 * @author gaoxs
 * 
 *         isNotBlank(Object obj) --判断枚举是否为空 isBlank(Object obj) --判断枚举是否为空
 *         isNumber(String number)--判断是否是数字
 */
public class EnumUtility {
    /**
     * 除重，用逗号合并
     * 
     * @param longStr
     * @param shortStr
     * @return
     */
    public static String replaceUrl(String longStr, String shortStr, String flag) {
	if (isBlank(shortStr)) {
	    return trimNumm(longStr);
	}
	if (isBlank(longStr)) {
	    return trimNumm(shortStr);
	}
	if (flag.equals("0")) {
	    if (longStr.indexOf(shortStr) > -1) {
		return longStr;
	    } else {

		return longStr + "," + shortStr;
	    }
	} else {

	    String[] shortStrs = shortStr.split(",");
	    for (String str : shortStrs) {
		if (isNotBlank(str)) {
		    if (longStr.indexOf(str) > -1) {
			longStr = longStr;
		    } else {

			longStr = longStr + "," + str;
		    }
		}
	    }
	    return longStr;
	}

    }

    public static String Encoding(String str, String newencode) {
	String oldencode = getEncoding(str);
	if (oldencode.equals(newencode)) {

	    return str;
	}
	try {
	    return new String(str.getBytes(oldencode), newencode);
	} catch (Exception exception) {
	    return str;
	}
    }

    /**
     * 判断编码方式
     * 
     * @param str
     * @return
     */
    public static String getEncoding(String str) {
	String encode = "GB2312";
	try {
	    if (str.equals(new String(str.getBytes(encode), encode))) {
		String s = encode;
		return s;
	    }
	} catch (Exception exception) {
	}
	encode = "ISO-8859-1";
	try {
	    if (str.equals(new String(str.getBytes(encode), encode))) {
		String s1 = encode;
		return s1;
	    }
	} catch (Exception exception1) {
	}
	encode = "UTF-8";
	try {
	    if (str.equals(new String(str.getBytes(encode), encode))) {
		String s2 = encode;
		return s2;
	    }
	} catch (Exception exception2) {
	}
	encode = "GBK";
	try {
	    if (str.equals(new String(str.getBytes(encode), encode))) {
		String s3 = encode;
		return s3;
	    }
	} catch (Exception exception3) {
	}
	return "";
    }

    /**
     * 处理null
     * 
     * @param str
     * @return
     */
    public static String trimNumm(String str) {
	if (isBlank(str)) {
	    return "";
	} else {
	    return str;
	}
    }

    /**
     * 判断枚举是否为空
     * 
     * @param o
     *            枚举类型
     * @return
     */
    public static boolean isNotBlank(Object obj) {

	if (obj == null || obj.equals("null")) {
	    return false;
	}
	if (obj.equals("")) {
	    return false;
	}
	return true;
    }

    /**
     * 判断是否为空
     * 
     * @param obj
     * @return
     */
    public static boolean isBlank(Object obj) {
	if (obj == null || obj.equals("null") || obj.equals("undefined")) {
	    return true;
	}
	if (obj.equals("")) {
	    return true;
	}
	return false;
    }

    /**
     * 判断是否是数字
     * 
     * @param args
     * @return
     */
    public static boolean isNumber(String number) {

	boolean rs = true;

	String test = "\\d+";

	if (number != null && Pattern.compile(test).matcher(number).matches()) {

	    rs = false;
	} else {

	    rs = true;
	}

	return rs;
    }

    /**
     * 得到n位长度的随机数 *
     * 
     * @param n随机数的长度
     * @return返回 n位的随机整数
     */
    public static String getRandomNumber(int n) {

	String number = "";
	Random rand = new Random();
	while (true) {
	    number = number + rand.nextInt(10);
	    if (number.length() >= n)
		break;
	}
	return number + "";
    }

    /**
     * 获取当前IP
     * 
     * @param request
     * @return
     */
    public static String getRemoteAddr(HttpServletRequest request) {
	String ip = request.getHeader("x-forwarded-for");
	if (isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
	    ip = request.getHeader("Proxy-Client-IP");
	}
	if (isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
	    ip = request.getHeader("WL-Proxy-Client-IP");
	}
	if (isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
	    ip = request.getRemoteAddr();
	}
	String[] iparray = ip.split(",");
	for (int i = 0; i < iparray.length; i++) {
	    ip = iparray[i];
	    if (!isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
		break;
	    }
	}
	return ip;
    }

    /**
     * byte数组转换成16进制字符串
     * 
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
	StringBuilder stringBuilder = new StringBuilder();
	if (src == null || src.length <= 0) {
	    return null;
	}
	for (int i = 0; i < src.length; i++) {
	    int v = src[i] & 0xFF;
	    String hv = Integer.toHexString(v);
	    if (hv.length() < 2) {
		stringBuilder.append(0);
	    }
	    stringBuilder.append(hv);
	}
	return stringBuilder.toString();
    }

    /**
     * 获取发送每次向通道发送的数据量
     * 
     * @return String
     */
    public static  String getRedisValue(String redisKey) {
	String value = RedisUtil.getString(redisKey); // 从redis获取
	if (StringUtils.isBlank(value)) { // 从redis获取失败
	    value = DictionaryUtils.getValueByTypeAndKey("redis", redisKey); // 从数据库配置中获取
	}
	if (StringUtils.isBlank(value)) { // 从redis和数据库获取失败
	    value = "-1"; // 默认数值
	}

	return value;
    }

}
